package com.example.ibsp_demo.service.impl;

import com.example.ibsp_demo.mapper.content.ContentMapper;
import com.example.ibsp_demo.mapper.content.ContentHotRankMapper;
import com.example.ibsp_demo.mapper.user.UserMapper;
import com.example.ibsp_demo.pojo.entity.content.Content;
import com.example.ibsp_demo.pojo.entity.user.User;
import com.example.ibsp_demo.service.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SearchServiceImpl implements ISearchService {

    @Autowired
    private ContentHotRankMapper contentHotRankMapper;

    @Autowired
    private ContentMapper contentMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public Object unifiedSearch(int type, String keyword) {
        // 预处理关键词
        String processedKeyword = preprocessKeyword(keyword);

        switch (type) {
            case 1:
                return userMapper.selectByUsernameLike(processedKeyword);
            case 2:
                return contentMapper.searchByKeyword(processedKeyword);
            case 3:
                return contentHotRankMapper.selectByKeywordOrderByHotScore(processedKeyword);
            default:
                throw new IllegalArgumentException("不支持的搜索类型: " + type);
        }
    }
    @Override
    public List<User> searchUsers(String keyword) {
        return userMapper.selectByUsernameLike(keyword);
    }

    @Override
    public List<Content> searchContents(String keyword) {
        return contentMapper.searchByKeyword(keyword);
    }

    private String preprocessKeyword(String keyword) {
        // 1. 去除首尾空格
        String processed = keyword.trim();

        // 2. 多个空格合并为一个
        processed = processed.replaceAll("\\s+", " ");

        // 可以添加更多的预处理逻辑
        return processed;
    }
    // 获取联想结果的方法
    @Override
    public List<String> getSuggestions(String keyword) {
        return contentMapper.searchTitlesByKeyword(keyword);
    }
}